#minutes
STEPS = 48*60*5


# --------------------
from numpy import *
from pylab import *
import mpl_toolkits.mplot3d.axes3d as p3
from bricks import *

TIME = [i for i in range (STEPS)]
CELL = []
REPORTERS = {}
EFFECTORS = {}
INDUCERS  = {}
RATIOS = {}
EVENTS = {}

####################### 
# GENE NETWORK ASSEMBLY
#######################
#
# IMPORTANT! PRODUCT-EFFECTOR: MANY-TO-ONE
#



#Effectors

#Pristinamicin binding protein
eff_pbs = Effector('P-BS', 'tal59krab.dat')
pct_pbs = Product('')
pct_pbs.bind(eff_pbs)
#eff_pbs.updateMass(50e-6 / GDA)

#Rapalog binding protein
eff_rbs = Effector('R-BS', 'tal95vp16_4.dat')
pct_rbs = Product('')
pct_rbs.bind(eff_rbs)
#pct_rbs.updateMass(50e-6 / GDA)


#TAL97:KRAB
eff_tal97 = Effector('TAL97:KRAB', 'tal59krab.dat')
pct_tal97_1 = Product('')
pct_tal97_1.bind(eff_tal97)
pct_tal97_2 = Product('')
pct_tal97_2.bind(eff_tal97)

#TAL57:VP16
eff_tal57 = Effector('TAL57:KRAB', 'tal59krab.dat')
pct_tal57_1 = Product('')
pct_tal57_1.bind(eff_tal57)
pct_tal57_2 = Product('')
pct_tal57_2.bind(eff_tal57)


#Inducers
pristinamicin = Inducer('Pristinamicin', eff_pbs, 0)
rapalog = Inducer('Rapalog', eff_rbs, 0)

#Reporters
rep_neptun = Product('Neptun')
rep_mcitrin = Product('mCitrin')


#Plasmids
gene1 = Gene('G1', float(sys.argv[1]), EXCON, 2, 8000)
gene1.addProduct(pct_pbs)
#CELL.append(gene1)

gene2 = Gene('G2', float(sys.argv[1]), EXCON, 2, 8000)
gene2.addProduct(pct_rbs)
#CELL.append(gene2)


gene3 = Gene('G3', float(sys.argv[1]), EXMIN, 2, 8000)
gene3.addEffector(eff_rbs)
gene3.addProduct(pct_tal57_1)
CELL.append(gene3)


gene4 = Gene('G4', float(sys.argv[1]), EXCON, 2, 8000)
gene4.addEffector(eff_pbs)
gene4.addProduct(pct_tal97_1)
CELL.append(gene4)


gene5 = Gene('G5', float(sys.argv[1]), EXCON, 2, 8000)
gene5.addEffector(eff_tal57)
gene5.addProduct(pct_tal97_2)
gene5.addProduct(rep_neptun)
CELL.append(gene5)

gene6 = Gene('G6', float(sys.argv[1]), EXCON, 2, 8000)
gene6.addEffector(eff_tal97)
gene6.addProduct(pct_tal57_2)
gene6.addProduct(rep_mcitrin)
CELL.append(gene6)




##################
# OBSERVED SPECIES
##################
REPORTERS[rep_neptun] = []
REPORTERS[rep_mcitrin] = []
INDUCERS[pristinamicin] = []
INDUCERS[rapalog] = []
#EFFECTORS[eff_rbs] = []
#EFFECTORS[eff_pbs] = []
EFFECTORS[eff_tal97] = []
EFFECTORS[eff_tal57] = []


####################
# TRANSFER FUNCTIONS
####################
#for k in EFFECTORS.keys():
# k.plotTransferFunction()
#show()  
#quit()  

###############
# EVENT MANAGER
###############
#e1 = Event(EVENTS, pristinamicin, 600, 5e-9)
#e2 = Event(EVENTS, rapalog, 1600, 5e-9)
#e3 = Event(EVENTS, rapalog, 10000, 5e-9)
#e4 = Event(EVENTS, pristinamicin, 11000, 5e-9)

#############################
# SIMULATION START; PLOT DATA
############################
for k in REPORTERS:
  RATIOS[k] = []
  

for t in TIME:
  if(EVENTS.has_key(t)):
    for ev in EVENTS[t]:
     ev.execute()

  for g in CELL:
    '''Run each gene once per time unit'''
    g.run()
    
  
  s = ''    
  for k in REPORTERS.keys():
    '''Measure reporters'''
    REPORTERS[k].append(k.getMass());
    RATIOS[k].append(k.getMass()/gene1.getMass());
    s += k.getTag() + ":"+str(k.getMass()) + " - "
  
 
  s = ''    
  for k in EFFECTORS.keys():
    '''Measure effectors'''
    EFFECTORS[k].append(k.getMass())
    s += k.getTag() + ":"+str(k.getMass()) + " - "
    #print s
  
  s = ''    
  for k in INDUCERS.keys():
    '''Measure effectors and tick trigger'''
    INDUCERS[k].append(k.getMass())
    s += k.getTag() + ":"+str(k.getMass()) + " - "
    k.tick()
    #print s
  

    
fig1 = figure()
ylabel("Amount [Da]")
xlabel("Time [min]")
for k in REPORTERS.keys():
  plot(TIME, REPORTERS[k], label=k.getTag())
for k in INDUCERS.keys():  
  plot(TIME, INDUCERS[k], label=k.getTag(), linestyle='dotted')
legend(loc="upper right")
  
fig2 = figure()  
for k in EFFECTORS.keys():
  plot(TIME, EFFECTORS[k], label=k.getTag(), linestyle='dashed')
  pass
legend(loc="upper right")
semilogy()    
  
fig3 = figure()  
for k in INDUCERS.keys():  
  plot(TIME, INDUCERS[k], label=k.getTag(), linestyle='dotted')
legend(loc="upper right")


#plot(TIME, [50e-6/1.660538e-24 for t in TIME], linestyle='dashed', color='red')
  
#semilogy()  
'''  
fig2 = figure()
ylabel("Mass Ratio Reporter/Gene1")
xlabel("Time [min]")
for k in REPORTERS.keys():
  plot(TIME, RATIOS[k], label=k.getTag()+" ratio 1")  
legend(loc="upper left")
'''  


'''Draw the configuration'''
print
for g in CELL:
  print g
show()
print "End."